#!/bin/bash

RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
NC='\033[0m'

INSTALL_PATH="$HOME/SillyTavern"
MANAGER_SCRIPT="$HOME/manager.sh"
BASHRC_FILE="$HOME/.bashrc"

REPO_CLONE_FLAG="$INSTALL_PATH/clone_complete.flag"

update_system() {
    apt update -y
    DEBIAN_FRONTEND=noninteractive apt upgrade -y -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold"
}

check_node_git() {
    if command -v node &> /dev/null; then
        node_version=$(node --version)
        echo -e "${GREEN}Found Node.js: $node_version${NC}"
    else
        echo -e "${YELLOW}Installing Node.js...${NC}"
        apt install -y nodejs npm
        ln -s /usr/bin/nodejs /usr/bin/node  # Fix Ubuntu node naming
        node_version=$(node --version)
        echo -e "${GREEN}Node.js installed: $node_version${NC}"
    fi

    if command -v git &> /dev/null; then
        git_version=$(git --version | cut -d ' ' -f 3)
        echo -e "${GREEN}Found Git: $git_version${NC}"
    else
        echo -e "${YELLOW}Installing Git...${NC}"
        apt install -y git
        git_version=$(git --version | cut -d ' ' -f 3)
        echo -e "${GREEN}Git installed: $git_version${NC}"
    fi
}

check_esbuild() {
    if ! command -v esbuild &> /dev/null; then
        echo -e "${YELLOW}Installing esbuild...${NC}"
        npm install -g esbuild
    fi
}

setup_sillytavern() {
    if [ ! -d "$INSTALL_PATH" ]; then
        echo -e "${YELLOW}Cloning SillyTavern...${NC}"
        git clone https://gitee.com/Summer100/SillyTavern.git "$INSTALL_PATH"
        cd "$INSTALL_PATH" || exit 1
        npm install
    else
        touch "$REPO_CLONE_FLAG"
        cd "$INSTALL_PATH" || exit 1
    fi
}


setup_manager_script() {
    echo -e "${YELLOW}正在下载管理脚本...${NC}"
    curl -o "$MANAGER_SCRIPT" https://gitee.com/Summer100/temp/raw/master/SillyTavern-manager.sh || { 
        echo -e "${RED}管理脚本下载失败${NC}"
        exit 1
    }
    chmod +x "$MANAGER_SCRIPT"
    echo -e "${GREEN}管理脚本下载完成并已设置执行权限${NC}"
}

setup_auto_start() {
    echo -e "${YELLOW}正在设置自动启动...${NC}"
    
    touch "$BASHRC_FILE"
    
    if ! grep -q "bash manager.sh" "$BASHRC_FILE"; then
        echo "bash manager.sh" >> "$BASHRC_FILE"
        echo -e "${GREEN}自动启动配置完成${NC}"
    else
        echo -e "${YELLOW}自动启动已经配置${NC}"
    fi
}

start_manager() {
    echo -e "${YELLOW}正在启动管理脚本...${NC}"
    bash "$MANAGER_SCRIPT" || {
        echo -e "${RED}管理脚本启动失败${NC}"
        exit 1
    }
}

main() {
    clear
    echo -e "${GREEN}-------------------------------------${NC}"
    echo -e "${GREEN}*     SillyTavern 安装程序         *${NC}"
    echo -e "${GREEN}-------------------------------------${NC}"
    echo -e "${YELLOW}开始安装 SillyTavern...${NC}"
    
    update_system
    check_node_git
    check_esbuild
    setup_sillytavern
    setup_manager_script
    setup_auto_start
    
    echo -e "${GREEN}-------------------------------------${NC}"
    echo -e "${GREEN}SillyTavern 安装完成！${NC}"
    echo -e "${GREEN}已设置自动启动${NC}"
    echo -e "${GREEN}正在启动管理脚本...${NC}"
    echo -e "${GREEN}-------------------------------------${NC}"
    
    start_manager
}

main